package net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class TcpServerTest1 {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);

        System.out.println("waiting for connect...");
        Socket socket = serverSocket.accept();
        System.out.println("A client connect...");

        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int len;
        StringBuilder sb = new StringBuilder();
        while ((len = inputStream.read(bytes)) != -1)
            sb.append(new String(bytes, 0, len));
        System.out.println("Client message: " + sb.toString());

        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("CCC".getBytes(StandardCharsets.UTF_8));

        socket.close();
        serverSocket.close();
    }
}
